﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using DTO;
using System.Data;

namespace DAO
{
    public static class PinDAO
    {
        public static databaseDataContext _db = new databaseDataContext();
        public static databaseDataContext getDB()
        {
            return new databaseDataContext();
        }

        //Hàm này lấy ra acquy có thông số Ah phù hợp
        public static object lay_danh_sach_pin(string _loaipin)
        {
            var query = from p in _db.PINs
                        where p.LOAIPIN.TenLoai == _loaipin
                        select new { p.MaPin, p.Vmin, p.Vmax, p.AMin, p.Amax, p.KichThuoc};

            return query.ToList();
        }


        public static List<LoaiPinDTO> Load_LoaiPin()
        {
            List<LoaiPinDTO> listLoaipin = new List<LoaiPinDTO>();
            var query = from p in _db.LOAIPINs
                        select new
                        {
                            p.MaLoaiPin,
                            p.TenLoai,
                            p.TienMoiwat,
                        };

            foreach (var item in query)
            {
                LoaiPinDTO loaipin = new LoaiPinDTO();
                loaipin.MaLoaiPin = item.MaLoaiPin;
                loaipin.TenLoai = item.TenLoai;
                loaipin.TienMoiWat = Convert.ToInt32(item.TienMoiwat) ;
                listLoaipin.Add(loaipin);
            }

            return listLoaipin;
        }


        public static LoaiPinDTO Load_LoaiPinTheoMa(int id)
        {
            LoaiPinDTO loaipin = new LoaiPinDTO();
            var item = _db.LOAIPINs.Single(p => p.MaLoaiPin == id);
            loaipin.TenLoai = item.TenLoai;
            loaipin.TienMoiWat = item.TienMoiwat.HasValue ? int.Parse(item.TienMoiwat.Value.ToString()) : 0;
            return loaipin;
        }

        public static void ThemLoaiPinMoi(LoaiPinDTO loaipin)
        {
            LOAIPIN _loaipin = new LOAIPIN();
            _loaipin.TenLoai = loaipin.TenLoai;
            _loaipin.TienMoiwat = loaipin.TienMoiWat;
            _db.LOAIPINs.InsertOnSubmit(_loaipin);
            _db.SubmitChanges();
           
        }


        public static void CapNhat_GiaTienPin(LoaiPinDTO  loaipin)
        {
            var item = _db.LOAIPINs.Single(p => p.MaLoaiPin == loaipin.MaLoaiPin );
            item.TenLoai = loaipin.TenLoai;
            item.TienMoiwat = loaipin.TienMoiWat;
            _db.SubmitChanges();
        }

        public static int LoadGiaPin(int MaLoaiPin)
        {
            
            var query = from p in _db.LOAIPINs
                        where p.MaLoaiPin == MaLoaiPin 
                        select new
                        {
                            p.TienMoiwat
                        };
            LoaiPinDTO loaipin = new LoaiPinDTO();
            foreach (var item in query)
            {

                loaipin.TienMoiWat = Convert.ToInt32(item.TienMoiwat);
            }

            return loaipin.TienMoiWat;
        }



        public static List<SanPham_PinDTO> search_pin(string text)
        {
            List<SanPham_PinDTO> listPin = new List<SanPham_PinDTO>();

            var query = from p in _db.PINs select p;

            int stt = 1;
            foreach (var item in query)
            {
                if (item.KichThuoc.Contains(text) || item.LOAIPIN.TenLoai.Contains(text) || item.Vmax.Value.ToString() == text || item.Vmin.Value.ToString() == text || item.Amax.Value.ToString() == text || item.AMin.Value.ToString() == text)
                {
                    SanPham_PinDTO pin = new SanPham_PinDTO();
                    pin.STT_Pin = stt;
                    pin.id = item.MaPin;
                    pin.KichThuoc = item.KichThuoc;
                    pin.VMax = item.Vmax.HasValue ? float.Parse(item.Vmax.Value.ToString()) : 0;
                    pin.VMin = item.Vmin.HasValue ? float.Parse(item.Vmin.Value.ToString()) : 0;
                    pin.AMax = item.Amax.HasValue ? float.Parse(item.Amax.Value.ToString()) : 0;
                    pin.AMin = item.AMin.HasValue ? float.Parse(item.AMin.Value.ToString()) : 0;
                    pin.tenLoaiPin = item.LOAIPIN.TenLoai;
                    pin.chinhsua = "Chỉnh sửa";
                    pin.xoa = "xóa";

                    listPin.Add(pin); stt++;
                }
            }

            return listPin;
        }

        public static SanPham_DaTa_Pin load_pin_id(int id)
        {
            SanPham_DaTa_Pin pin = new SanPham_DaTa_Pin();
            var _pin = _db.PINs.Single(p => p.MaPin == id);
            pin.KichThuoc = _pin.KichThuoc;
            pin.VMax = _pin.Vmax.HasValue ? int.Parse(_pin.Vmax.Value.ToString()) : 0;
            pin.VMin = _pin.Vmin.HasValue ? int.Parse(_pin.Vmin.Value.ToString()) : 0;
            pin.AMax = _pin.Amax.HasValue ? int.Parse(_pin.Amax.Value.ToString()) : 0;
            pin.AMin = _pin.AMin.HasValue ? int.Parse(_pin.AMin.Value.ToString()) : 0;
            pin.LoaiPin = _pin.LOAIPIN.MaLoaiPin;

            return pin;
        }

        public static void Insert_SanPham_Pin(SanPham_DaTa_Pin pin)
        {
            PIN _pin = new PIN();
            var loaipin = _db.LOAIPINs.Single(p => p.MaLoaiPin == pin.LoaiPin);
            _pin.KichThuoc = pin.KichThuoc;
            _pin.Vmax = pin.VMax;
            _pin.Vmin = pin.VMin;
            _pin.Amax = pin.AMax;
            _pin.AMin = pin.AMin;
            _pin.LOAIPIN = loaipin;

            _db.PINs.InsertOnSubmit(_pin);
            _db.SubmitChanges();
        }

        public static void Delete_SanPham_Pin(int id)
        {
            try
            {
                var pin = _db.PINs.Single(p => p.MaPin == id);
                _db.PINs.DeleteOnSubmit(pin);
                _db.SubmitChanges();
            }
            catch { }
        }

        public static void Update_SanPham_Pin(SanPham_DaTa_Pin pin)
        {
            try
            {
                var _pin = _db.PINs.Single(p => p.MaPin == pin.id);
                var _loaipin = _db.LOAIPINs.Single(p => p.MaLoaiPin == pin.LoaiPin);
                _pin.KichThuoc = pin.KichThuoc;
                _pin.Vmax = pin.VMax;
                _pin.Vmin = pin.VMin;
                _pin.Amax = pin.AMax;
                _pin.AMin = pin.AMin;
                _pin.LOAIPIN = _loaipin;

                _db.SubmitChanges();
            }
            catch { }
        }

        public static void XoaLoaiPin(int id)
        {
            var loaipin = _db.LOAIPINs.Single(p => p.MaLoaiPin == id);
            _db.LOAIPINs.DeleteOnSubmit(loaipin);
            _db.SubmitChanges();
        }








    }
}
